package com.shidaimanong.demo.lottery;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 学编程，关注：十代码农
 * 抖音、快手、B站、西瓜视频、今日头条
 */
public class LotteryDemo0 {

    public static void main(String[] args) {
        //读取文件中存放的所有人名
        List<String> nameList = readFile("花名册.txt");
        //生成随机数
        Random random = new Random();
        int randomIndex = random.nextInt(nameList.size());
        //打印随机选中的人名
        System.out.println(nameList.get(randomIndex));
    }

    /**
     * 读文件内容
     * @param fileName
     * @return
     */
    public static List<String> readFile(String fileName) {
        BufferedReader in = null;
        List<String> list = new ArrayList<>();
        try {
            in = new BufferedReader(new FileReader(fileName));
            String str;
            while ((str = in.readLine()) != null) {
                list.add(str);
            }
        } catch (IOException e) {
            System.out.print(e.getMessage());
        } finally {
            try {
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }
}